home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmarray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  5.5 KB  |  128 lines

  1. // CmArray.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Array definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMARRAY_H
  10. #define _CMARRAY_H
  11.  
  12. #include <cm/include/cmcont.h>
  13.  
  14. class CmArrayIterator;                       // Iterator class stub.
  15.  
  16. class CmArray : public CmContainer {         // Array class definition.
  17. public:
  18.   CmArray(unsigned = 0, unsigned = 0);       // Default array constructor.
  19.   CmArray(const CmArray&);                   // Array copy constructor.
  20.  ~CmArray();                                 // Array destructor.
  21.  
  22.   CmArray&  operator= (const CmArray&);      // Assignment operator.
  23.   CmObject* operator[](int) const;           // Indexing operator.
  24.                                              // (Cannot be used as lvalue).
  25.  
  26.   void        delta      (unsigned);         // Set delta value.
  27.   unsigned    delta      () const;           // Get delta value.
  28.   int         total      () const;           // Return number of objects.
  29.   CmObject*   at         (int) const;        // Get object at index.
  30.   Bool        add        (CmObject*);        // Append object to array.
  31.   Bool        insertAt   (int, CmObject*);   // Insert object at index.
  32.   Bool        replaceAt  (int, CmObject*);   // Replace object at index.
  33.   Bool        remove     (CmObject*);        // Remove equal object.
  34.   Bool        removeAt   (int);              // Remove object at index.
  35.   int         index      (CmObject*) const;  // Get index of object.
  36.   CmObject*   lookup     (CmObject*) const;  // Look for equal object.
  37.   Bool        contains   (CmObject*) const;  // Object is in array?
  38.   unsigned    occurrences(CmObject*) const;  // How many occurrences?
  39.   void        removeAll  ();                 // Remove all objects.
  40.   Bool        resize     (unsigned);         // Resize the array.
  41.   Bool        isEmpty    () const;           // Is array empty?
  42.   void        quickSort  ();                 // Quick sort the array.
  43.   CmIterator* newIterator() const;           // Get array iterator.
  44.   
  45.   Bool write(CmReserveFile&) const;          // Write to reserve file.
  46.   Bool read (CmReserveFile&);                // Read from reserve file.
  47.  
  48.   CMOBJECT_DEFINE(CmArray, CmContainer)      // Define object funcs.
  49.  
  50. protected:
  51.   static int cmpObjs(const void*, const void*); // Quick sort compare func.
  52.  
  53.   unsigned   _delta;                         // Delta value.
  54.   unsigned   _total;                         // Number of objects.
  55.   CmObject **_entries;                       // Array of object pointers.
  56.   friend     CmArrayIterator;                // Iterator can access,
  57. };
  58.  
  59. class CmArrayIterator : public CmIterator {  // Iterator definition.
  60. public:
  61.   CmArrayIterator(const CmArray& A)          // Iterator constructor.
  62.                  : _array(A), _index(0) {}
  63.  
  64.   Bool      done    () const;                // Check if done iterating.
  65.   CmObject* next    ();                      // Return and advance.
  66.   CmObject* previous();                      // Return and backup.
  67.   CmObject* current () const;                // Return current object.
  68.   void      first   ();                      // Move to first object.
  69.   void      last    ();                      // Move to last object.
  70.  
  71.   CMOBJECT_DEFINE(CmArrayIterator, CmIterator)  // Define object funcs.
  72.  
  73. protected:
  74.   const CmArray& _array;                     // Array being iterated.
  75.   int            _index;                     // Current array index.
  76.   friend         CmArray;                    // Array class can access.
  77. };
  78.  
  79. // "delta" sets a new delta value for automatic growing.
  80. inline void CmArray::delta(unsigned dt)
  81. { _delta = dt; }
  82.  
  83. // "delta" returns the current delta value.
  84. inline unsigned CmArray::delta() const
  85. { return _delta; }
  86.  
  87. // "total" returns the number of objects added to this array.
  88. inline int CmArray::total() const
  89. { return _total; }
  90.  
  91. // "at" returns the object at the specified index.
  92. inline CmObject* CmArray::at(int idx) const
  93. { return (idx >= 0 && idx < _total) ? _entries[idx] : NULL; }
  94.  
  95. // "[]" returns the object at the specified index.  Cannot be used as lvalue.
  96. inline CmObject* CmArray::operator[](int idx) const
  97. { return at(idx); }
  98.  
  99. // "contains" checks if the array contains an object equal to the input.
  100. inline Bool CmArray::contains(CmObject* pObj) const
  101. { return (index(pObj) > -1) ? TRUE : FALSE; }
  102.  
  103. // "done" checks to see if we can iterate any further.
  104. inline Bool CmArrayIterator::done() const
  105. { return (_index >= _array._total || _index < 0); }
  106.  
  107. // "next" returns the current object and advances the iterator.
  108. inline CmObject* CmArrayIterator::next()
  109. { return (_index < (_array._total)) ? _array._entries[_index++] : NULL; }
  110.  
  111. // "previous" returns the current object and decrements the iterator.
  112. inline CmObject* CmArrayIterator::previous()
  113. { return (_index >= 0) ? _array._entries[_index--] : NULL; }
  114.  
  115. // "current" returns the current object pointed to by the iterator.
  116. inline CmObject* CmArrayIterator::current() const
  117. { return (_index < _array._total) ? _array._entries[_index] : NULL; }
  118.  
  119. // "first" moves the iterator to the first object.
  120. inline void CmArrayIterator::first()
  121. { _index = 0; }
  122.  
  123. // "last" moves the iterator to the last object.
  124. inline void CmArrayIterator::last()
  125. { _index = _array._total-1; }
  126.  
  127. #endif
  128.